<template>
  <el-upload
      ref="incrementVersionUpload"
      v-model:fileList="fileList"
      action="#"
      :fileList="fileList"
      :auto-upload="false"
      :multiple="false"
      :limit="1"
      :on-exceed="handleExceed"
      :before-upload="fileBeforeUpload"
      accept=".xlsx"
  >
    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
    <div slot="tip" class="el-upload__tip">上传文件只能是xlsx格式</div>
  </el-upload>
</template>

<script setup>
import {defineProps, defineModel, defineEmits, defineOptions, ref} from "vue"
import {ElMessage, ElUpload} from "element-plus"
defineOptions({ name: 'CommonImport' })

const props = defineProps({
  formOption: Object,
  rowForm: Object,
  action: String,
  execute: Function,
  row: Object,
  _table: Object,
})

const formData = defineModel()
const visible = defineModel('visible')
const params = defineModel('params')
const emit = defineEmits(['search'])

function submit() {
  if (fileList.value.length !== 1) {
    ElMessage.warning('请先选择文件')
    return false
  } else {
    params.value = {file: fileList.value[0].raw}
    return true
  }
}

defineExpose({submit})

const fileList = ref([])

function handleExceed() {
  ElMessage.warning(`附件最多上传一个，请删除后再上传`)
}

function fileBeforeUpload(file) {
  const fileSuffix = file.name.substring(file.name.lastIndexOf(".") + 1);
  const whiteList = ["xlsx"];
  if (whiteList.indexOf(fileSuffix) === -1) {
    ElMessage.warning("上传文件只能是xlsx格式");
    return false;
  }
  return true;
}
</script>

<style scoped lang="scss">

</style>
